package com.epfresh.supplier.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.TagPager;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.MinusRecordAdapter;
import com.epfresh.supplier.entity.MinusRecordBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderMinusRecordActivity extends BaseActivity {//} implements LoadMoreExpandableListView.IXListViewListener{
    private ListView mListView;
    private ArrayList<MinusRecordBean.ContentBean.ListBean> list;
    Context mContext;
    MinusRecordAdapter adapter;
    protected View vException;
    protected TextView tvExceptionDescription;
    protected ImageView ivExceptionIcon;
    private ProgressBar pb;
    private int requestSucceedCount = 0;
    private int PAGE_SIZE = 5;
    private TagPager tagPager = new TagPager();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        list = new ArrayList<MinusRecordBean.ContentBean.ListBean>();
        mContext = this;
        id = getIntent().getStringExtra("id");
        baseback.setVisibility(View.VISIBLE);
        requestOrderList(0);
    }

    void initView(){
        setContentView(R.layout.activity_record_minus);
        mListView = (ListView) findViewById(R.id.listview);
        basetitle.setText("减免退记录");
        rightbtn.setText("帮助");
        rightbtn.setOnClickListener(this);
        vException = findViewById(R.id.v_exception);
        initException();
    }


    public void initException() {
        tvExceptionDescription = (TextView) vException.findViewById(R.id.tv_exception);
        ivExceptionIcon = (ImageView) vException.findViewById(R.id.iv_exception);
        pb = (ProgressBar) vException.findViewById(R.id.pb);
    }


    public void showMainView() {
        vException.setVisibility(View.GONE);
        mainView.setVisibility(View.VISIBLE);
    }


    public void showExceptionView(int imgSrc, String text, int exceptionType) {
        if (imgSrc > 0) {

        }
        pb.setVisibility(View.GONE);
        vException.setVisibility(View.VISIBLE);
        tvExceptionDescription.setVisibility(View.VISIBLE);
        ivExceptionIcon.setVisibility(View.VISIBLE);
        mainView.setVisibility(View.GONE);

        ivExceptionIcon.setImageResource(imgSrc);
        tvExceptionDescription.setText(text);
        tvExceptionDescription.setTag(exceptionType);
        ivExceptionIcon.setTag(exceptionType);
        tvExceptionDescription.setOnClickListener(this);
        ivExceptionIcon.setOnClickListener(this);
    }
    public void showProgressView() {

        pb.setVisibility(View.VISIBLE);
        vException.setVisibility(View.VISIBLE);
        tvExceptionDescription.setVisibility(View.GONE);
        ivExceptionIcon.setVisibility(View.GONE);
        mainView.setVisibility(View.GONE);
    }


    OnRequestListener<MinusRecordBean> onRequestListener = new OnRequestListener<MinusRecordBean>() {
        @Override
        public MinusRecordBean jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, MinusRecordBean.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            RequestTag requestTag = (RequestTag) tag;
            if (requestSucceedCount == 0){
                showExceptionView(R.mipmap.order_no, "网络问题,请点击重试", HttpRequest.FAIL_NET_FAULT);
            }
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            if (requestSucceedCount == 0){
                showExceptionView(R.mipmap.order_no, arg + "", HttpRequest.FAIL_DATA_FAULT);//arg + ",请点击重试"
            }
            RequestTag requestTag = (RequestTag) tag;
        }

        @Override
        public void onStart(Object tag) {
            if (requestSucceedCount == 0){
                showProgressView();
            }
        }

        @Override
        public void onResponse(ResponseEntity<MinusRecordBean> response, Object tag) {
            showMainView();
            requestSucceedCount ++;
            RequestTag requestTag = (RequestTag) tag;
            if (requestTag.action == 0) {
                list.clear();
            }
            MinusRecordBean orderResponse = response.getResponseElement();
            List<MinusRecordBean.ContentBean.ListBean> content = orderResponse.getContent();
            TextView tvNumber = (TextView) findViewById(R.id.tv_number);
            TextView tvTime = (TextView) findViewById(R.id.tv_time);
            TextView tvMoneyNumber = (TextView) findViewById(R.id.tv_money_number);
            tvNumber.setText("订单号："+orderResponse.getOrderNo());
            tvTime.setText("下单时间："+orderResponse.getPlaceOrderTime());
            tvMoneyNumber.setText(orderResponse.getJmtPrice()+"元");

            list.addAll(content);
            adapter = new MinusRecordAdapter(mContext, list);
            mListView.setAdapter(adapter);

            //adapter.notifyDataSetChanged();
            if(list.size()==0){
                showExceptionView(R.mipmap.order_no, "暂无订单", HttpRequest.FAIL_DATA_EMPTY);
            }

        }
    };


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.baseback:
                finish();
                break;
        }
    }

    private RequestTag requestTag = new RequestTag();
    String cgiName= "supplier/jmt/list";
    String id;

    public void requestOrderList(int page) {

        RequestEntitySupply<Map<String, String>> entity = new RequestEntitySupply<>();
        Map<String, String> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        //map.put("pageNumber", page + "");
        map.put("orderId",id);
        entity.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_jmt_list);
        entity.setParameters(map);
        requestTag.tag = com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_jmt_list;
        requestTag.action = page;
        request(entity, requestTag, onRequestListener);
    }
}
